<template>
  <div class="account-container">
    <div class="header">
      <div class="left">
        <h2>收款账户管理</h2>
      </div>
    </div>

    <el-card class="form-card">
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="开户银行" prop="bankName">
          <el-input
            v-model="form.bankName"
            placeholder="请输入开户银行"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item label="银行卡号" prop="bankCardNumber">
          <el-input
            v-model="form.bankCardNumber"
            placeholder="请输入银行卡号"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item label="支付宝账号" prop="alipayAccount">
          <el-input
            v-model="form.alipayAccount"
            placeholder="请输入支付宝账号"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item label="微信账号" prop="wechatAccount">
          <el-input
            v-model="form.wechatAccount"
            placeholder="请输入微信账号"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="submitting"
            @click="handleSubmit"
          >
            保存
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'StudentAccountForm'
}
</script>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getMyAccount, saveAccount } from '@/api/student/account'

const formRef = ref(null)
const submitting = ref(false)

const form = ref({
  bankName: '',
  bankCardNumber: '',
  alipayAccount: '',
  wechatAccount: ''
})

const rules = {
  bankName: [
    { required: true, message: '请输入开户银行', trigger: 'blur' }
  ],
  bankCardNumber: [
    { required: true, message: '请输入银行卡号', trigger: 'blur' }
  ]
}

// 获取账户信息
const fetchAccount = async () => {
  try {
    const response = await getMyAccount()
    if (response) {
      form.value = {
        bankName: response.bankName || '',
        bankCardNumber: response.bankCardNumber || '',
        alipayAccount: response.alipayAccount || '',
        wechatAccount: response.wechatAccount || ''
      }
    }
  } catch (error) {
    console.error('Failed to fetch account:', error)
    ElMessage.error('获取账户信息失败')
  }
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    submitting.value = true

    await saveAccount(form.value)
    ElMessage.success('保存成功')
  } catch (error) {
    console.error('Submit failed:', error)
    ElMessage.error(error.response?.data || '保存失败')
  } finally {
    submitting.value = false
  }
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchAccount()
})
</script>

<style scoped>
.account-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 16px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.left {
  display: flex;
  align-items: center;
  gap: 16px;

  h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #303133;
  }
}

.form-card {
  margin-bottom: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}
</style> 